import { logger } from '../core/logger';
import { logInfo } from '../models/logInfo';
import { clientBase, HOST } from './clientBase';
import { ajaxResult } from '../models/ajaxResult';
import { uploadInfo } from '../models/uploadInfo';
import axios, { AxiosError, AxiosResponse } from '@ohos/axios';

class analysisClient extends clientBase {
  getDeviceData(deviceId: string, dateTime: Date): Promise<ajaxResult<Array<uploadInfo>>> {
    return new Promise((resolve) => {
      let date = `${dateTime.getFullYear()}-${(dateTime.getMonth() + 1)}-${dateTime.getDate()}`;
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/analysis/getDeviceData?deviceId=${deviceId}&dateTime=${date}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<uploadInfo> = response.data.result;
          resolve(this.buildSuccessResult<Array<uploadInfo>>(ret));
        } else
          resolve(this.buildFailedResult<Array<uploadInfo>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<uploadInfo>>(err));
      });
    });
  }

  getAreaData(areaId: string, dateTime: Date): Promise<ajaxResult<Array<uploadInfo>>> {
    return new Promise((resolve) => {
      let date = `${dateTime.getFullYear()}-${(dateTime.getMonth() + 1)}-${dateTime.getDate()}`;
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/analysis/getAreaData?areaId=${areaId}&dateTime=${date}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<uploadInfo> = response.data.result;
          resolve(this.buildSuccessResult<Array<uploadInfo>>(ret));
        } else
          resolve(this.buildFailedResult<Array<uploadInfo>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<uploadInfo>>(err));
      });
    });
  }

  getLogs(deviceId: string, dateTime: Date): Promise<ajaxResult<Array<logInfo>>> {
    return new Promise((resolve) => {
      let date = `${dateTime.getFullYear()}-${(dateTime.getMonth() + 1)}-${dateTime.getDate()}`;
      axios.get<string, AxiosResponse<string>, null>(`${HOST}api/analysis/getLogs?deviceId=${deviceId}&dateTime=${date}`, this.withToken).then((response: AxiosResponse) => {
        if (response.status == 200) {
          let ret: Array<logInfo> = response.data.result;
          resolve(this.buildSuccessResult<Array<logInfo>>(ret));
        } else
          resolve(this.buildFailedResult<Array<logInfo>>(response.statusText));
      }).catch((err: AxiosError) => {
        logger.instance.error(err);
        resolve(this.buildExceptionResult<Array<logInfo>>(err));
      });
    });
  }
}
export default new analysisClient();